package GUI.Productos;

import BE.BEProducto;
import BE.BEUsuario;
import BL.BLCombo;
import BL.BLProducto;
import BL.BLValidaciones;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;


public class GUIMantenimientoProducto extends javax.swing.JDialog {
    private boolean busqueda;
    private BEProducto objProducto;
    private BEUsuario objUsuario;

    public BEProducto getObjProducto() {
        return objProducto;
    }

    public void setObjProducto(BEProducto objProducto) {
        this.objProducto = objProducto;
    }





    /** Creates new form GUIMantenimientoProducto */
    public GUIMantenimientoProducto(java.awt.Frame parent, boolean modal, BEUsuario usuario) {
        super(parent, modal);
        objUsuario=usuario;
        initComponents();
        this.inicializa();
    }


     public GUIMantenimientoProducto(java.awt.Frame parent, boolean modal, BEProducto producto) {
        super(parent, modal);
        initComponents();
        this.inicializa();
        objProducto=producto;
        busqueda= true;
        if (busqueda) this.modoBusqueda();

        }

    private void modoBusqueda() {
        btnAgregar.setVisible(false);
        btnEditar.setVisible(false);
        btnEliminar.setText("Aceptar");
        cmbEstado.setSelectedIndex(1);
        cmbEstado.setEnabled(false);
    }





    public void inicializa()
    {        
        this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
        BLCombo blCombo = new BLCombo();
        //Formatos para componentes
        tblProductos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //Llenar Combos
        blCombo.setCombo("TIPOPRODUCTO",0,cmbTipoProducto);
        blCombo.setCombo("CONTACTO",0,cmbProveedor);
        cmbEstado.addItem(new BLCombo(2,"TODOS"));
        cmbEstado.addItem(new BLCombo(1,"ACTIVO"));
        cmbEstado.addItem(new BLCombo(0,"INACTIVO"));
    }

    public void limpiar()
    {
        BLProducto blProd = new BLProducto();

        blProd.limpiaTabla(tblProductos);
        cmbEstado.setSelectedIndex(0);
         if (!busqueda)
              cmbEstado.setSelectedIndex(0);
        cmbProveedor.setSelectedIndex(0);
        txtNombre.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBuscarProducto = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        cmbTipoProducto = new javax.swing.JComboBox();
        lblTipoProducto = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        btnFiltrar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        lblProveedor = new javax.swing.JLabel();
        cmbProveedor = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();
        pnlBotones = new javax.swing.JPanel();
        btnAgregar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mantenimiento de Productos");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("GUIMantenimientoProducto"); // NOI18N

        pnlBuscarProducto.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar Producto"));

        lblNombre.setText("Nombre:");

        lblEstado.setText("Estado:");

        lblTipoProducto.setText("Tipo de Producto:");

        btnFiltrar.setText("Filtrar");
        btnFiltrar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnFiltrar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnFiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiltrarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        lblProveedor.setText("Proveedor:");

        javax.swing.GroupLayout pnlBuscarProductoLayout = new javax.swing.GroupLayout(pnlBuscarProducto);
        pnlBuscarProducto.setLayout(pnlBuscarProductoLayout);
        pnlBuscarProductoLayout.setHorizontalGroup(
            pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarProductoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarProductoLayout.createSequentialGroup()
                        .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNombre)
                            .addComponent(lblEstado))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                            .addComponent(cmbEstado, 0, 218, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTipoProducto)
                            .addComponent(lblProveedor))
                        .addGap(18, 18, 18)
                        .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cmbProveedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbTipoProducto, 0, 172, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBuscarProductoLayout.createSequentialGroup()
                        .addComponent(btnFiltrar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnlBuscarProductoLayout.setVerticalGroup(
            pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarProductoLayout.createSequentialGroup()
                .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarProductoLayout.createSequentialGroup()
                        .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNombre))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEstado)
                            .addComponent(lblProveedor)))
                    .addComponent(lblTipoProducto)
                    .addGroup(pnlBuscarProductoLayout.createSequentialGroup()
                        .addComponent(cmbTipoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmbProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlBuscarProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFiltrar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Producto", "Tipo", "Stock", "Proveedor", "Descripcion", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblProductos.setAutoscrolls(false);
        jScrollPane1.setViewportView(tblProductos);

        btnAgregar.setText("Agregar");
        btnAgregar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAgregar.setMinimumSize(new java.awt.Dimension(70, 25));
        btnAgregar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnEditar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnEliminar.setMinimumSize(new java.awt.Dimension(70, 25));
        btnEliminar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setMinimumSize(new java.awt.Dimension(70, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBotonesLayout = new javax.swing.GroupLayout(pnlBotones);
        pnlBotones.setLayout(pnlBotonesLayout);
        pnlBotonesLayout.setHorizontalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBotonesLayout.createSequentialGroup()
                .addContainerGap(81, Short.MAX_VALUE)
                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlBotonesLayout.setVerticalGroup(
            pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .addComponent(pnlBuscarProducto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlBotones, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlBuscarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addGap(11, 11, 11)
                .addComponent(pnlBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        GUIAgregarProducto frmAgregarProducto = new GUIAgregarProducto(null, true,objUsuario);
        frmAgregarProducto.setResizable(false);
        frmAgregarProducto.setVisible(true);

        this.limpiar();
        BLProducto blProd = new BLProducto();
        blProd.buscaProd("",0,0,0, tblProductos,0); //PARA BUSCAR TODOS LOS PRODUCTOS
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        int fila;
        int columna = 0; //Columna ID
        int idProd;

        fila = tblProductos.getSelectedRow();

        if (fila < 0)
            JOptionPane.showMessageDialog(null,"Debe seleccionar un producto para editar",
                                          "Informacion",JOptionPane.INFORMATION_MESSAGE);
        else if (JOptionPane.showConfirmDialog(null,"Desea editar el producto?",
                 "Confirmacion",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE) == 0)
        {
            idProd = Integer.parseInt(tblProductos.getModel().getValueAt(fila,columna).toString());

            GUIModificarProducto frmModificarProducto = new GUIModificarProducto(null, true,idProd,objUsuario);
            frmModificarProducto.setResizable(false);
            frmModificarProducto.setVisible(true);

            BLProducto blProd = new BLProducto();
            this.limpiar();
            blProd.buscaProd("",0,0,0,tblProductos,0); //PARA BUSCAR TODOS LOS PRODUCTOS
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnFiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiltrarActionPerformed
        BLProducto blProd = new BLProducto();
        BLValidaciones validar = new BLValidaciones();
        //Obtener los IDs de estado y de tipo de producto
        BLCombo idEstado = (BLCombo)cmbEstado.getSelectedItem();
        BLCombo idTipo = (BLCombo)cmbTipoProducto.getSelectedItem();
        BLCombo idProveedor = (BLCombo)cmbProveedor.getSelectedItem();

        blProd.buscaProd(validar.validarSqlInjection(txtNombre.getText()),
                         idEstado.id,idTipo.id,idProveedor.id,tblProductos,1); //1 = busqueda por paramtros
    }//GEN-LAST:event_btnFiltrarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (busqueda)
        {
            BLProducto objGestorProducto = new BLProducto();
            objProducto= objGestorProducto.llenaVentana(Integer.parseInt(tblProductos.getModel().getValueAt(tblProductos.getSelectedRow(),0).toString()));
            this.setVisible(false);
        }
        else 
        {
            BLProducto bLProducto = new BLProducto();
            bLProducto.eliminarProd(tblProductos,objUsuario);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        this.limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUIMantenimientoProducto dialog = new GUIMantenimientoProducto(new javax.swing.JFrame(), true,(BEProducto)null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnFiltrar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbProveedor;
    private javax.swing.JComboBox cmbTipoProducto;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblProveedor;
    private javax.swing.JLabel lblTipoProducto;
    private javax.swing.JPanel pnlBotones;
    private javax.swing.JPanel pnlBuscarProducto;
    private javax.swing.JTable tblProductos;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

}
